import Vue from 'vue'
import Vuex from 'vuex'
import ui from './modules/ui'
import connection from './modules/connection'
import config from './modules/config'
import modules from './modules/modules'
import httpClient from './modules/httpClient'
import httpServer from './modules/httpServer'
import mqtt from './modules/mqtt'
import history from './modules/history'
import tcpUdp from './modules/tcpUdp'

Vue.use(Vuex)

export default new Vuex.Store({
  modules: {
    ui,
    connection,
    config,
    modules,
    httpClient,
    httpServer,
    mqtt,
    history,
    tcpUdp
  },
  
  state: {
    environments: [],
    activeEnvironment: null,
    globalSettings: {
      theme: 'light',
      language: 'zh-CN',
      autoSave: true,
      requestTimeout: 30000,
      maxHistoryItems: 1000
    }
  },
  
  mutations: {
    SET_ENVIRONMENTS(state, environments) {
      state.environments = environments
    },
    
    SET_ACTIVE_ENVIRONMENT(state, environment) {
      state.activeEnvironment = environment
    },
    
    UPDATE_GLOBAL_SETTINGS(state, settings) {
      state.globalSettings = { ...state.globalSettings, ...settings }
    }
  },
  
  actions: {
    loadEnvironments({ commit }) {
      const saved = localStorage.getItem('environments')
      if (saved) {
        try {
          const environments = JSON.parse(saved)
          commit('SET_ENVIRONMENTS', environments)
        } catch (e) {
          console.error('Failed to load environments:', e)
        }
      }
    },
    
    saveEnvironments({ state }) {
      localStorage.setItem('environments', JSON.stringify(state.environments))
    },
    
    addEnvironment({ commit, state }, environment) {
      const environments = [...state.environments, environment]
      commit('SET_ENVIRONMENTS', environments)
      localStorage.setItem('environments', JSON.stringify(environments))
    },
    
    updateEnvironment({ commit, state }, { id, data }) {
      const environments = state.environments.map(env => 
        env.id === id ? { ...env, ...data } : env
      )
      commit('SET_ENVIRONMENTS', environments)
      localStorage.setItem('environments', JSON.stringify(environments))
    },
    
    deleteEnvironment({ commit, state }, id) {
      const environments = state.environments.filter(env => env.id !== id)
      commit('SET_ENVIRONMENTS', environments)
      localStorage.setItem('environments', JSON.stringify(environments))
    },
    
    setActiveEnvironment({ commit }, environment) {
      commit('SET_ACTIVE_ENVIRONMENT', environment)
      localStorage.setItem('activeEnvironment', environment ? environment.id : '')
    },
    
    loadGlobalSettings({ commit }) {
      const saved = localStorage.getItem('globalSettings')
      if (saved) {
        try {
          const settings = JSON.parse(saved)
          commit('UPDATE_GLOBAL_SETTINGS', settings)
        } catch (e) {
          console.error('Failed to load global settings:', e)
        }
      }
    },
    
    updateGlobalSettings({ commit }, settings) {
      commit('UPDATE_GLOBAL_SETTINGS', settings)
      localStorage.setItem('globalSettings', JSON.stringify(settings))
    },
    
    saveAllConfigs({ dispatch }) {
      dispatch('saveEnvironments')
      dispatch('config/saveConfig')
      dispatch('history/saveHistory')
    }
  }
})